load("//go:def.bzl", "go_binary", "go_library")
load("//go/private:common.bzl", "RULES_GO_REPO_NAME")
load("//go/tools/bazel_testing:def.bzl", "go_bazel_test")

go_library(
    name = "gopackagesdriver_lib",
    srcs = [
        "bazel.go",
        "bazel_json_builder.go",
        "build_context.go",
        "driver_request.go",
        "flatpackage.go",
        "json_packages_driver.go",
        "main.go",
        "packageregistry.go",
        "utils.go",
    ],
    importpath = "github.com/bazelbuild/rules_go/go/tools/gopackagesdriver",
    visibility = [
        "//tests/integration/gopackagesdriver:__pkg__",
    ],
)

go_binary(
    name = "gopackagesdriver",
    embed = [":gopackagesdriver_lib"],
    visibility = ["//visibility:public"],
    x_defs = {
        # Determine the repository part of labels pointing into the rules_go repo. This is needed
        # both to specify the aspect and to match labels in query output.
        "rulesGoRepositoryName": RULES_GO_REPO_NAME,
    },
)

RULES_GO_REPO_NAME_FOR_TEST = RULES_GO_REPO_NAME if RULES_GO_REPO_NAME != "@" else "@io_bazel_rules_go"

go_bazel_test(
    name = "gopackagesdriver_test",
    size = "enormous",
    srcs = ["gopackagesdriver_test.go"],
    embed = [":gopackagesdriver_lib"],
    env = {
        "GOPACKAGESDRIVER_BAZEL_COMMON_FLAGS": "--noexperimental_enable_bzlmod",
    },
    rule_files = ["//:all_files"],
    x_defs = {
        "rulesGoRepositoryName": RULES_GO_REPO_NAME_FOR_TEST,
    },
)

filegroup(
    name = "all_files",
    testonly = True,
    srcs = glob(["**"]),
    visibility = ["//visibility:public"],
)
